package com.xjt.query.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.xjt.common.StringUtils;
import com.xjt.query.dao.interf.QueryUserDao;
import com.xjt.query.dao.pojo.QueryUser;

public class QueryUserDaoImpl extends SqlMapClientDaoSupport implements QueryUserDao {

    @Override
    public Long insertUser(QueryUser user) {
        Object newKey = getSqlMapClientTemplate().insert("query_user.insertUser", user);
        return (Long) newKey;
    }

    @Override
    public int deleteUserById(long id) {
        int rows = getSqlMapClientTemplate().delete("query_user.deleteUserById", id);
        return rows;
    }

    @Override
    public int deleteUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return 0;
        }
        int rows = getSqlMapClientTemplate().delete("query_user.deleteUserByUserId", userId);
        return rows;
    }

    @SuppressWarnings("unchecked")
    @Override
    public QueryUser selectUserById(long id) {
        QueryUser user = new QueryUser();
        user.setId(id);
        List<QueryUser> list = getSqlMapClientTemplate().queryForList("query_user.queryUserList", user);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public QueryUser selectUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        QueryUser user = new QueryUser();
        user.setUserId(userId);
        List<QueryUser> list = getSqlMapClientTemplate().queryForList("query_user.queryUserList", user);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
   public int countUserList(QueryUser user) {
       Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("query_user.countUserList", user);
       return count;
   }

    @SuppressWarnings("unchecked")
    @Override
    public List<QueryUser> queryUserList(QueryUser user) {
        List<QueryUser> list = getSqlMapClientTemplate().queryForList("query_user.queryUserList", user);
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<QueryUser> queryUserListPaging(QueryUser user) {
        if(null == user){
            user = new QueryUser();
        }
        user.doPage(user.getPageNo(), countUserList(user));
        List<QueryUser> list = getSqlMapClientTemplate().queryForList("query_user.queryUserListPaging", user);
        return list;
    }

    @Override
    public int updateUserById(QueryUser user, long id) {
        if (null == user) {
            return 0;
        }
        user.setId(id);
        int rows = getSqlMapClientTemplate().update("query_user.updateUserById", user);
        return rows;
    }

    @Override
    public int updateUserByUserId(QueryUser user, String userId) {
        if (null == user || StringUtils.isBlank(userId)) {
            return 0;
        }
        user.setUserId(userId);
        int rows = getSqlMapClientTemplate().update("query_user.updateUserByUserId", user);
        return rows;
    }

}
